<html>
<head>

<title>Groovy Goodness: Add Java-style Listener Support with ListenerList Annotation</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Add Java-style Listener Support with ListenerList Annotation</h3>

<div class="post">
<p>To implement a bean event pattern in Java a class must be able to register or remove event listeners. Then the list of listeners can be used to fire methods defined by the listeners. Setting up a list of listeners, providing the correct add and remove listener methods, add fire event methods is all boilerplate code we have to write each time. With Groovy we don't have to, we simply apply the <code>@ListenerList</code> annotation to a typed collection field in our class.</p>
<p>Groovy will add the add and remove listener methods, a method to get the assigned listeners and methods to fire event methods defined in the listener interface. All this is done before compilation, so the resulting class file contains all the necessary code and is therefore usable in plain Java code without any Groovy dependencies. Notice the collection field must have generic type, because otherwise the correct code cannot be generated.
</p>
<pre class="brush:groovy">
// Simple listener interface with spoke() method.
interface TalkListener {
    void spoke(TalkEvent event)
}

// Event to be passed into listener spoke() method.
class TalkEvent {
    String text, origin
}

class Speaker {
    @groovy.beans.ListenerList
    List&lt;TalkListener&gt; talkListeners

    String name

    void sayHello() {
        // fireSpoke is added by @ListenerList.
        fireSpoke(new TalkEvent(origin: name, text: 'Hello Groovy world!'))
    }

    // Methods generated by @ListenerList:
    // void addTalkListener(TalkListener)
    // void removeTalkListener(TalkListener)
    // TalkListener[] getTalkListeners()
    // void fireSpoke(TalkEvent)
}


def s = new Speaker(name: 'mrhaki')
// Add listener interface implementation to simply output
// the data in the event object.
s.addTalkListener([spoke: { event -&gt;
   println "$event.origin says '${event.text.toLowerCase()}'"
}] as TalkListener)
// Second implemenation of listener interface is added.
def shouter = { event -&gt; println "${event.text.toUpperCase()}" } as TalkListener
s.addTalkListener shouter
s.sayHello()

// Output:
// mrhaki says 'hello groovy world!'
// HELLO GROOVY WORLD!
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>